package com.forxindu.kit.box;

import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ViewKit {

    /**
     * <b>setListViewHeightBasedOnChildren。</b>
     * <p>
     * <b>详细说明：</b>
     * </p>
     * <!-- 在此添加详细说明 --> 无。
     *
     * @param listView
     */
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        Log.e("", "ListView Height:" + params.height);
        listView.setLayoutParams(params);
    }

    public static void showToast(Context context, String content) {
        Toast toast =  Toast.makeText(context, null, Toast.LENGTH_SHORT);
        toast.setText(content);
        toast.show();
    }

    public static void showCenterToast(Context context, String content) {
        Toast toast = Toast.makeText(context, null, Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setText(content);
        toast.show();
    }
}
